package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dao.TbBusinessDao;
import com.itheima.dto.ClueQueryDto;
import com.itheima.pojo.TbBusiness;
import com.itheima.pojo.TbClue;
import com.itheima.service.TbBusinessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 线索(TbBusiness)表服务实现类
 *
 * @author makejava
 * @since 2022-01-16 15:48:53
 */
@Service("tbBusinessService")
public class TbBusinessServiceImpl implements TbBusinessService {
    @Resource
    private TbBusinessDao tbBusinessDao;


    @Override
    public R insertClue(TbClue tbClue) {
        String s = JSON.toJSONString(tbClue);
        TbBusiness tbBusiness = JSON.parseObject(s, TbBusiness.class);
        int insert = tbBusinessDao.insert(tbBusiness);
        if (insert>0) {
            return new R(1,"添加商机成功!",null);
        }
        return new R(0,"添加商机失败!",null);
    }

    @Override
    public R page(ClueQueryDto clueQueryDto) {
        IPage<TbBusiness> pageInfo=new Page<>(clueQueryDto.getPage(),clueQueryDto.getPageSize());
        LambdaQueryWrapper<TbBusiness> lqw=new LambdaQueryWrapper<>();
        lqw.like(clueQueryDto.getName()!=null,TbBusiness::getName,clueQueryDto.getName());
        tbBusinessDao.selectPage(pageInfo,lqw);
        return R.success("商机分页信息",pageInfo);
    }

    @Override
    public R insertBusiness(TbBusiness tbBusiness) {
        int insert = tbBusinessDao.insert(tbBusiness);
        if (insert>0){
            return R.success("添加商机成功!",null);
        }
        return R.error("添加商机失败!");
    }

    @Override
    public R getById(Long id) {
        TbBusiness tbBusiness = tbBusinessDao.selectById(id);
        return R.success("商机详情信息",tbBusiness);
    }

    @Override
    public R update(TbBusiness tbBusiness) {
        int i = tbBusinessDao.updateById(tbBusiness);
        if (i>0) {
            return R.success("更新商机成功!",null);
        }
        return R.error("更新商机失败!");

    }

    @Override
    public R updateStatus(Long id,String status) {
        Integer updateStatus =tbBusinessDao.updateStatus(id,status);
        if (updateStatus != null) {
            return R.success("更新商机状态成功!",null);
        }
        return R.error("更新商机状态失败!");

    }

}
